package com.hyh.core.web;

import com.hyh.core.po.HyhJob;
import com.hyh.core.po.Person;
import com.hyh.log.annotation.HyhLog;
import com.hyh.quartz.helper.JobHelper;
import com.hyh.rocketmq.helper.RocketMqHelper;
import com.hyh.sso.LoginStatus;
import com.hyh.sso.po.LoginResult;
import com.hyh.sso.po.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 控制层代码逻辑
 *
 * @Author: heyuhua
 * @Date: 2021/1/7 11:33
 */
@RestController
@RequestMapping("hyh")
public class HyhController {

    @Resource
    private RocketMqHelper rocketMqHelper;

    @Autowired
    private JobHelper jobHelper;

    @HyhLog(value = "hello world test")
    @RequestMapping(value = "hello", method = RequestMethod.GET)
    public String hello() {
        return "hello";
    }

    @HyhLog(value = "send mq message test")
    @RequestMapping(value = "sendMessage", method = RequestMethod.GET)
    public String sendMessage() {
        Person person = new Person();
        person.setName("heyuhua");
        person.setAge(25);
        rocketMqHelper.asyncSend("PERSON_ADD", MessageBuilder.withPayload(person).build());
        return "sendMessage";
    }

    @HyhLog(value = "login test")
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public LoginResult login(@RequestBody LoginUser loginUser) {
        Assert.notNull(loginUser.getAccount(), "account is null");
        Assert.notNull(loginUser.getPassword(), "password is null");
        LoginResult loginResult = new LoginResult(LoginStatus.SUCCESS);
        loginResult.setLoginUser(loginUser);
        //模拟直接返回登录成功
        return loginResult;
    }

    @HyhLog(value = "start job test")
    @RequestMapping(value = "startJob", method = {RequestMethod.POST, RequestMethod.GET})
    public void startJob() {
        String name = "hyh-name", group = "hyh-group", cron = "*/5 * * * * ?";
        jobHelper.start(name, group, cron, HyhJob.class);
    }

    @HyhLog(value = "delete jod test")
    @RequestMapping(value = "deleteJob", method = {RequestMethod.POST, RequestMethod.GET})
    public void deleteJob() {
        String name = "hyh-name", group = "hyh-group", cron = "*/5 * * * * ?";
        jobHelper.delete(name, group);
    }
}
